package com.yswee.framework.utils;

import java.util.Locale;
import java.util.Locale.Category;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;

public class LangUtils {
	
	public static Locale getLocale() {
		Locale locale;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			locale = Locale.getDefault(Category.DISPLAY);
		} else {
			locale = Locale.getDefault();
		}
		return locale;
	}
	
	public static void updateLocale(Context context) {
		Locale locale = getLocale();
		updateLocale(context, locale);
	}
	
	@SuppressWarnings("deprecation")
	public static void updateLocale(Context context, Locale locale) {
		if (context == null) {
			return;
		}
		Resources resources = context.getResources();
		DisplayMetrics dm = resources.getDisplayMetrics();
		Configuration config = resources.getConfiguration();

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			config.setLocale(locale);
		} else {
			config.locale = locale;
		}
		resources.updateConfiguration(config, dm);
	}
	
}